package cn.itcast.bos.web.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.domain.bean.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import freemarker.template.Configuration;
import freemarker.template.Template;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class PromotiaonAction extends ActionSupport implements ModelDriven<Promotion> {
   private static boolean CREATE_HTML = false;
private Promotion promotion=new Promotion();
   //模型驱动
	@Override
	public Promotion getModel() {
		
		return promotion;
	}
	//属性驱动
	private int page;
	private int rows;
	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	@Action(value="promotion_pageQuery",results={@Result(name="success",type="json")})
      public String queryPage(){
		PageBean<Promotion> pageBean = WebClient.create(Constants.BOS_MANAGEMENT_HOST+"/bos_management/services/promotionService/"
				+ "promotion?page="+page+"&rows="+rows)
				.accept(MediaType.APPLICATION_JSON).get(PageBean.class);
		//压入值栈
		ServletActionContext.getContext().getValueStack().push(pageBean);
    	  return SUCCESS;
      }
	@Action(value = "promotion_showDetail")
	public String showDetail() throws Exception {
		System.out.println("进来没");
		// 先判断 id 对应html 是否存在，如果存在 直接返回
		String htmlRealPath = ServletActionContext.getServletContext()
				.getRealPath("/freemarker");
		File htmlFile = new File(htmlRealPath + "/" + promotion.getId() + ".html");
		
		

		// 如果html文件不存在 ，查询数据库，结合freemarker模板生成 页面
		if (CREATE_HTML || !htmlFile.exists()) {
			Configuration configuration = new Configuration(
					Configuration.VERSION_2_3_22);
			configuration.setDirectoryForTemplateLoading(new File(
					ServletActionContext.getServletContext().getRealPath(
							"/WEB-INF/freemarker_templates")));
			// 获取模板对象
			Template template = configuration
					.getTemplate("promotion_detail.ftl");

			// 动态数据
			Promotion promotions = WebClient
					.create(Constants.BOS_MANAGEMENT_HOST
							+ "/bos_management/services/promotionService/promotion/"
							+ promotion.getId()).accept(MediaType.APPLICATION_JSON)
					.get(Promotion.class);  //  /bos_management
			System.out.println(promotions);
			Map<String, Object> parameterMap = new HashMap<String, Object>();
			parameterMap.put("promotion",promotions);

			// 合并输出
			template.process(parameterMap, new OutputStreamWriter(
					new FileOutputStream(htmlFile), "utf-8"));

		}
		
		CREATE_HTML = false;

		// 存在 ，直接将文件返回
		ServletActionContext.getResponse().setContentType(
				"text/html;charset=utf-8");
		//读文件，写网络
		FileUtils.copyFile(htmlFile,ServletActionContext.getResponse().getOutputStream()); 

//		ServletActionContext.getResponse().getWriter().println("<h1>写给浏览器的</h1>");
		
		return NONE;
	}
}
